package page
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.ui.Keyboard;
	
	import skeleton.AccelerometerHelper;
	import skeleton.AccelerometerStatus;
	import skeleton.VisualKeyboard;
	
	public class Page_game extends Page
	{
		[Embed( source = "/work_Master_and_Comander.swf.swf", mimeType="application/x-shockwave-flash")]
		private const SWF_GAME:Class;

		[Embed( source = "/arrow_left.png", mimeType="image/png")]
		private const IMG_LEFT:Class;

		[Embed( source = "/arrow_right.png", mimeType="image/png")]
		private const IMG_RIGHT:Class;

		[Embed( source = "/button.png", mimeType="image/png")]
		private const IMG_BUTTON:Class;

		private var _state:int;
		private static const STATE_ENTER:int = 0;
		private static const STATE_RUNNING:int = 1;
		private static const STATE_DONE:int = 4;

		private var _nodeGame:Sprite = new Sprite();
		private var _nodeKeyboard:Sprite = new Sprite();

		private var _game:MovieClip;
		private var _gameWidth:Number;
		private var _gameHeight:Number;
		
		private var _keyboard:VisualKeyboard;
		private var _keyboardLeft:Sprite;
		private var _keyboardRight:Sprite;
		private var _keyboardSpace:Sprite;
		
		private var _accelerometer:AccelerometerHelper;
		private var _accelerometerStatus:AccelerometerStatus;
		
		public function Page_game(parent:DisplayObjectContainer)
		{
			super(parent);
			
			addChild(_nodeGame);
			addChild(_nodeKeyboard);
		}
		
		override protected function onEnterFrame(e:Event):void
		{
			super.onEnterFrame(e);

			var scaleX:Number = stage.stageWidth / _gameWidth;
			var scaleY:Number = stage.stageHeight / _gameHeight;
			if(scaleX != _nodeGame.scaleX || scaleY != _nodeGame.scaleY)
			{
				_nodeGame.scaleX = scaleX;
				_nodeGame.scaleY = scaleY;
			}
			
			var scale:Number = Math.min(stage.stageWidth, stage.stageHeight) / 1024;
			_keyboard.setScale(scale);

			if(_state == STATE_ENTER)
			{
				_state = STATE_RUNNING;
			}
			else if(_state == STATE_RUNNING)
			{
				
			}
			else if(_state == STATE_DONE)
			{
			}
			
			//have to do this at last
			if(_state == STATE_DONE)
			{
			}
		}
		
		override public function enter():void
		{
			super.enter();
			_state == STATE_ENTER;
			
			createGame();
			createInputPanel();
			createAccelerometer();
		}
		
		private function createGame():void
		{
			_game = new SWF_GAME();
			_nodeGame.addChild(_game);
			_gameWidth = _game.width;
			_gameHeight = _game.height;
		}
		
		private function createInputPanel():void
		{
			_keyboard = new VisualKeyboard(_nodeKeyboard);
			_keyboard.setAlpha(0.5);
			_keyboardLeft = _keyboard.addButton(new IMG_LEFT(), 64, stage.stageHeight - 96, Keyboard.LEFT, addFilter);
			_keyboardRight = _keyboard.addButton(new IMG_RIGHT(), 128 + 64, stage.stageHeight - 96, Keyboard.RIGHT, addFilter);
			_keyboardSpace = _keyboard.addButton(new IMG_BUTTON(), stage.stageWidth - 128, stage.stageHeight - 128, Keyboard.SPACE, addFilter);
			
			_global.setVisualKeyboard(_keyboard);
			_keyboard.setVisible(false);
			
			function addFilter(display:DisplayObject):void
			{
				display.filters = [new GlowFilter(0x0000ff, 1, 24, 24, 16)];
			}
		}
		
		private function createAccelerometer():void
		{
			_accelerometer = new AccelerometerHelper(onUpdate);
			
			var mask:int;
			if(_accelerometer.isActived())
			{
				mask = AccelerometerStatus.MODE_KEYBOARD | AccelerometerStatus.MODE_ACCELEROMETER;
			}
			else
			{
				mask = AccelerometerStatus.MODE_KEYBOARD;
			}

			_accelerometerStatus = new AccelerometerStatus(mask, cbSwitchToKeyBoard, cdSwitchToAccelerometer);
			_global.setAccelerometerStatus(_accelerometerStatus);
			
			if(_accelerometer.isActived())
			{
				_accelerometerStatus.switchToAccelerometer();
			}
			else
			{
				_accelerometerStatus.switchToKeyboard();
			}

			function onUpdate(time:Number, acceX:Number, acceY:Number, acceZ:Number):void
			{
				var threshold:Number = 0.17364817766693034885171662676931; //10 degree

				if(acceX < -threshold)
				{
					csKey.setStatus(Keyboard.RIGHT, 1);
				}
				else
				{
					csKey.setStatus(Keyboard.RIGHT, 0);
				}
				
				if(acceX > threshold)
				{
					csKey.setStatus(Keyboard.LEFT, 1);
				}
				else
				{
					csKey.setStatus(Keyboard.LEFT, 0);
				}

/*				else
				{
					if(csKey.isDown(Keyboard.LEFT) && acceX > -0.1)
					{
						csKey.setStatus(Keyboard.LEFT, 0);
					}
					
					if(csKey.isDown(Keyboard.RIGHT) && acceX < 0.1)
					{
						csKey.setStatus(Keyboard.RIGHT, 0);
					}
				}
*/
				
/*				if(acceZ > 0.5)
				{
					csKey.setStatus(Keyboard.SPACE, 1);
				}
				else
				{
					csKey.setStatus(Keyboard.SPACE, 0);
				}
*/				
			}
			
			function cbSwitchToKeyBoard():void
			{
				_keyboardLeft.visible = true;
				_keyboardRight.visible = true;

				_accelerometer.disable();
			}
			
			function cdSwitchToAccelerometer():void
			{
				_keyboardLeft.visible = false;
				_keyboardRight.visible = false;

				_accelerometer.enable();
			}
			
		}
		
		override public function exit():void
		{
			_global.setVisualKeyboard(null);

			//
			super.exit();
		}
	}
}